package org.power.bus.controller;

import com.alibaba.fastjson.JSONObject;
//import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.power.bus.entity.Persion;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author 苗冬冬
 */
@Controller
public class TestController {

    @CrossOrigin
    @GetMapping("/test")
    @ResponseBody
    public Persion test(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        System.out.println(request.getParameter("username"));
        Persion p = new Persion();
        p.setPassword("awds1314");
        p.setUsername("miao");
        return p;
    }

    @CrossOrigin
    @PostMapping("/test2")
    @ResponseBody
    public Persion test2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");

        BufferedReader reader = request.getReader();
        String line = reader.readLine();
        System.out.println(line);
        JSONObject json = JSONObject.parseObject(line);
        System.out.println("json=>" + json.getString("username"));
        Persion jsonP = JSONObject.parseObject(json.toJSONString(), Persion.class);
        System.out.println(jsonP);

        Persion p = new Persion();
        p.setPassword("awds1314");
        p.setUsername("MengHua");
        return p;
    }

    @CrossOrigin
    @PostMapping("/test3")
    @ResponseBody
    public Persion test3(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");

        System.out.println(request.getParameter("username"));

        Persion p = new Persion();
        p.setPassword("awds1314");
        p.setUsername("MengHua");
        return p;
    }

    //@Test
    public void json() {
        //System.out.println("miao_dong_dong");
    }
}
